package demo1;

/**
 * @Author liangzai
 * @Description:
 */
public class Test {
    public boolean isBalanced(TreeNode root) {
        if(root == null) return true;

        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);
        return false;
    }

    public TreeNode invertTree(TreeNode root) {
        if(root == null) return null;
        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;

        invertTree(root.left);
        invertTree(root.right);
        return root;
    }


    public boolean isBalanced(TreeNode root) {
        if(root == null) return true;

        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);

        return Math.abs(leftHeight - rightHeight) <= 1
                && isBalanced(root.left) && isBalanced(root.right);

    }


    public boolean isBalanced2(TreeNode root) {
        if(root == null) return true;
        return maxDepth(root) >= 0;
    }

    public int maxDepth(TreeNode root) {
        if(root == null) return 0;
        int leftHeight = maxDepth(root.left);
        if(leftHeight < 0) {
            return -1;
        }
        int rightHeight = maxDepth(root.right);
        if(leftHeight >= 0 &&  rightHeight >= 0 &&

        return Math.abs(leftHeight - rightHeight) <= 1
                && isBalanced(root.left) && isBalanced(root.right);

    }


    public boolean isBalanced2(TreeNode root) {
        if(root == null) return true;
        return maxDepth(root) >= 0;
    }

    public int maxDepth(TreeNode root) {
        if(root == null) return 0;
        int leftHeight = maxDepth(root.left);
        if(leftHeight < 0) {
            return -1;
        }
        int rightHeight = maxDepth(root.right);
        if(leftHeight >= 0 &&  rightHeight >= 0 &&
                Math.abs(leftHeight - rightHeight) <= 1) {
            return Math.max(leftHeight,rightHeight) + 1;
        }else {
            return -1;
        }
    }

    public boolean isSymmetric(TreeNode root) {
        if(root == null) return true;
        return isSymmetricChile(root.left,root.right);
    }

    public boolean isSymmetricChile(TreeNode leftTree,TreeNode rightTree) {
        if(leftTree == null && rightTree != null ||
                leftTree != null && rightTree == null) {
            return false;
        }
        if(leftTree == null && rightTree == null ) {
            return true;
        }
        Math.abs(leftHeight - rightHeight) <= 1) {
            return Math.max(leftHeight,rightHeight) + 1;
        }else {
            return -1;
        }
    }

    public boolean isSymmetric(TreeNode root) {
        if(root == null) return true;
        return isSymmetricChile(root.left,root.right);
    }

    public boolean isSymmetricChile(TreeNode leftTree,TreeNode rightTree) {
        if(leftTree == null && rightTree != null ||
                leftTree != null && rightTree == null) {
            return false;
        }
        if(leftTree == null && rightTree == null ) {
            return true;
        }
        if(leftTree.val != rightTree.val) {
            return false;
        }
        return isSymmetricChile(leftTree.left,rightTree.right)
                && isSymmetricChile(leftTree.right,rightTree.left);
    }

    public void levelOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            System.out.print(cur.val+" ");
            if(cur.left != null) {
                queue.offer(cur.left);
            }
            if(cur.right != null) {
                if(leftTree.val != rightTree.val) {
                    return false;
                }
                return isSymmetricChile(leftTree.left,rightTree.right)
                        && isSymmetricChile(leftTree.right,rightTree.left);
            }

            public void levelOrder(TreeNode root) {
                if(root == null) {
                    return;
                }
                Queue<TreeNode> queue = new LinkedList<>();
                queue.offer(root);
                while (!queue.isEmpty()) {
                    TreeNode cur = queue.poll();
                    System.out.print(cur.val+" ");
                    if(cur.left != null) {
                        queue.offer(cur.left);
                    }
                    if(cur.right != null) {
                        queue.offer(cur.right);
                    }
                }
            }


   /* public List<List<Integer>> levelOrder2(TreeNode root) {
        List<List<Integer>> ret = new ArrayList<>();
        if(root == null) {
            return ret;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            List<Integer> tmpList= new ArrayList<>();
            int size = queue.size();
            while (size != 0) {
                TreeNode cur = queue.poll();
                size--;
                //System.out.print(cur.val+" ");
                queue.offer(cur.right);
            }
        }
    }


}
